<template>
  <div id="app">
    <Topbar :key="topBarKeyValue"></Topbar>
    <Header></Header>
    <div class="box">
      <router-view @updateCart="updateTop" />
    </div>
    <Footer></Footer>

    <!-- 登录 -->
    <Login v-show="isShowModal"></Login>
    <!-- 提示组件 -->
    <transition name="slide">
      <Toast v-show="isShowToast"></Toast>
    </transition>
  </div>
</template>

<script>
import Topbar from "./components/Topbar.vue";
import Footer from "./components/Footer.vue";
import Header from "./components/Header.vue";
import Login from "./components/Login";
import Toast from "./components/Toast.vue";

// import { JingPinAPI, ReMenAPI } from "./request/api";
import { mapMutations, mapState } from "vuex";

export default {
  data() {
    return {
      // 组件是否更新的值
      topBarKeyValue: 1,
    };
  },
  components: {
    Topbar,
    Header,
    Footer,
    Login,
    Toast,
  },
  computed: {
    ...mapState({
      isShowModal: (state) => state.showModal.isShowModal,
      isShowToast: (state) => state.showToast.isShowToast,
    }),
  },
  methods: {
    ...mapMutations({
      changeShowToast: "showToast/changeShowToast",
    }),
    updateTop() {
      this.topBarKeyValue++;
    },
  },
  watch: {
    // 监听路由的变化
    "$route.path": {
      handler(newVal, oldVal) {
        // console.log(newVal, oldVal);
        // 防止触发俩次created 避免发送重复请求
        if (oldVal !== "/") {
          // key属性的值一变化，就会做组件重载，从而执行created函数
          console.log("组件重载！！！！");
          this.topBarKeyValue++;
        }
      },
    },
  },
};
</script>

<style lang="less" scoped>
.app {
  width: 1980px;
}
// .box {
//   margin-bottom: 240px;
// }
/* 入场的起始状态 = 离场的结束状态 */
.slide-enter,
.slide-leave-to {
  opacity: 0;
}

.slide-enter-active,
.slide-leave-active {
  transition: opacity 0.3s linear;
}

.slide-enter-to,
.slide-leave {
  opacity: 1;
}
</style>
